/* Copyright(c) 2010- Siro-info Co.(http://www.siro-info.com)
 * All rights reserved.
 */
package com.siro.pm.ui.detail;

import com.siro.pm.I18nManager;
import com.siro.pm.model.DemandOrBug;
import com.siro.pm.model.Developer;
import com.siro.pm.model.Project;
import com.siro.pm.model.provider.DemandOrBugProvider;
import com.siro.pm.ui.main.BugsPage;
import com.siro.pm.utils.DeveloperBrowser;
import com.siro.pm.utils.UUIDGenerator;
import com.vaadin.terminal.Sizeable;
import com.vaadin.ui.AbstractSelect.Filtering;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.ComboBox;
import com.vaadin.ui.GridLayout;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;

/**
 * Class Declaration:
 * 
 * @version v1.0
 * @since BugDetailPage.java v1.0
 */
public class BugDetailPage extends VerticalLayout {
	private static final long serialVersionUID = 6225106255250310462L;

	private Window window;
	private BugsPage bugsPage;
	private Project ownerProject;
	private DemandOrBug bug;

	private TextField name;
	private TextField desc;
	private TextField project;
	private ComboBox status;
	private ComboBox level;
	private TextField creator;
	private TextField solver;
	private TextField advisor;
	private TextField result;
	private TextField createtime;
	private TextField closetime;
	private Button button_Save;

	ComboBox creatorDeveloper;
	ComboBox solverDeveloper;
	ComboBox advisorDeveloper;

	public BugDetailPage() {
	}

	public BugDetailPage(Project ownerProject, DemandOrBug bug) {
		this.ownerProject = ownerProject;
		this.bug = bug;
	}

	public void createContents() {
		setMargin(true);
		setSpacing(true);

		// base info
		GridLayout bugBaseInfo = new GridLayout();
		bugBaseInfo.setStyleName("h2");
		bugBaseInfo.setCaption(I18nManager.getMessage("bug.base.info"));
		bugBaseInfo.setColumns(2);
		bugBaseInfo.setMargin(true);
		bugBaseInfo.setSpacing(true);
		bugBaseInfo.setWidth("100%");

		name = new TextField(DemandOrBugProvider.BUG_PROPERTY_NAME);
		name.setWidth("100%");
		bugBaseInfo.addComponent(name);

		desc = new TextField(DemandOrBugProvider.BUG_PROPERTY_DESC);
		desc.setWidth("100%");
		bugBaseInfo.addComponent(desc);

		project = new TextField(DemandOrBugProvider.BUG_PROPERTY_PROJECT);
		project.setWidth("100%");
		bugBaseInfo.addComponent(project);

		level = new ComboBox(DemandOrBugProvider.BUG_PROPERTY_LEVEL);
		String[] levelData = DemandOrBugProvider.BUG_LEVEL_DATA;
		for (int i = 0; i < levelData.length; i++) {
			level.addItem(levelData[i]);
		}
		level.setWidth("100%");
		level.setFilteringMode(Filtering.FILTERINGMODE_CONTAINS);
		level.setImmediate(true);
		bugBaseInfo.addComponent(level);

		createtime = new TextField(DemandOrBugProvider.BUG_PROPERTY_CREATETIME);
		createtime.setWidth("100%");
		bugBaseInfo.addComponent(createtime);

		HorizontalLayout creatorLayout = new HorizontalLayout();
		creatorLayout.setWidth("100%");
		creator = new TextField(DemandOrBugProvider.BUG_PROPERTY_CREATOR);
		creator.setWidth("100%");
		creatorLayout.addComponent(creator);
		creatorLayout.setExpandRatio(creator, 1);
		creatorDeveloper = new ComboBox(" ");
		DeveloperBrowser.configDeveloperBrowser(creatorDeveloper,
				bug == null ? ownerProject : bug.getProject());
		creatorLayout.addComponent(creatorDeveloper);
		bugBaseInfo.addComponent(creatorLayout);

		HorizontalLayout solverLayout = new HorizontalLayout();
		solverLayout.setWidth("100%");
		solver = new TextField(DemandOrBugProvider.BUG_PROPERTY_SOLVER);
		solver.setWidth("98%");
		solverLayout.addComponent(solver);
		solverLayout.setExpandRatio(solver, 1);
		solverDeveloper = new ComboBox(" ");
		DeveloperBrowser.configDeveloperBrowser(solverDeveloper,
				bug == null ? ownerProject : bug.getProject());
		solverLayout.addComponent(solverDeveloper);
		bugBaseInfo.addComponent(solverLayout);

		HorizontalLayout advisorLayout = new HorizontalLayout();
		advisorLayout.setWidth("100%");
		advisor = new TextField(DemandOrBugProvider.BUG_PROPERTY_ADVISOR);
		advisor.setWidth("98%");
		advisorLayout.addComponent(advisor);
		advisorLayout.setExpandRatio(advisor, 1);
		advisorDeveloper = new ComboBox(" ");
		DeveloperBrowser.configDeveloperBrowser(advisorDeveloper,
				bug == null ? ownerProject : bug.getProject());
		advisorLayout.addComponent(advisorDeveloper);
		bugBaseInfo.addComponent(advisorLayout);

		// process info
		GridLayout projectProcessInfo = new GridLayout();
		projectProcessInfo.setStyleName("h2");
		projectProcessInfo.setCaption(I18nManager
				.getMessage("bug.process.info"));
		projectProcessInfo.setColumns(2);
		projectProcessInfo.setMargin(true);
		projectProcessInfo.setSpacing(true);
		projectProcessInfo.setWidth("100%");

		status = new ComboBox(DemandOrBugProvider.BUG_PROPERTY_STATUS);
		String[] statusData = DemandOrBugProvider.BUG_STATUS_DATA;
		for (int i = 0; i < statusData.length; i++) {
			status.addItem(statusData[i]);
		}
		status.setWidth("100%");
		status.setFilteringMode(Filtering.FILTERINGMODE_CONTAINS);
		status.setImmediate(true);
		projectProcessInfo.addComponent(status);

		result = new TextField(DemandOrBugProvider.BUG_PROPERTY_RESULT);
		result.setWidth("100%");
		projectProcessInfo.addComponent(result);

		closetime = new TextField(DemandOrBugProvider.BUG_PROPERTY_CLOSETIME);
		closetime.setWidth("100%");
		projectProcessInfo.addComponent(closetime);

		// buttons
		HorizontalLayout buttons = new HorizontalLayout();
		buttons.setMargin(true);
		buttons.setSpacing(true);
		buttons.setWidth("100%");

		button_Save = new Button(I18nManager.getMessage("button.save"));
		button_Save.setWidth(50, Sizeable.UNITS_PIXELS);
		button_Save.addListener(new SaveClickListener());
		buttons.addComponent(button_Save);
		buttons.setComponentAlignment(button_Save, Alignment.MIDDLE_RIGHT);

		addComponent(bugBaseInfo);
		addComponent(projectProcessInfo);
		addComponent(buttons);

		initData();
	}

	private void initData() {
		project.setEnabled(false);
		if (bug != null) {
			name.setValue(bug.getName());
			desc.setValue(bug.getDesc());
			project.setValue(bug.getProject().getName());
			creator.setValue(bug.getCreator().getName());
			solver.setValue(bug.getSolver().getName());
			level.select(bug.getLevel());
			status.select(bug.getStatus());
			advisor.setValue(bug.getAdvisor().getName());
			result.setValue(bug.getResult());
			createtime.setValue(bug.getCreatetime());
			closetime.setValue(bug.getClosetime());

			name.setEnabled(false);
			creator.setEnabled(false);
			creatorDeveloper.setEnabled(false);
			createtime.setEnabled(false);
			if (!Developer.isProjectManager(bug.getProject().getManager())) {
				level.setEnabled(false);
				solver.setEnabled(false);
				solverDeveloper.setEnabled(false);
				advisor.setEnabled(false);
				advisorDeveloper.setEnabled(false);
			}
			if (!DemandOrBug.couldEditBug(bug)) {
				desc.setEnabled(false);
				status.setEnabled(false);
				result.setEnabled(false);
				closetime.setEnabled(false);
				button_Save.setEnabled(false);
			}
		}
		if (ownerProject != null) {
			project.setValue(ownerProject.getName());
		}
	}

	class SaveClickListener implements ClickListener {
		private static final long serialVersionUID = -2851840644753744125L;

		public void buttonClick(ClickEvent event) {
			boolean update = true;
			if (bug == null) {
				bug = new DemandOrBug();
				bug.setId(UUIDGenerator.newUUID());
				update = false;
			}
			bug.setName((String) name.getValue());
			bug.setDesc((String) desc.getValue());
			if (ownerProject != null)
				bug.setProject(ownerProject);
			bug.setLevel((String) level.getValue());
			Developer developer = (Developer) creatorDeveloper.getValue();
			if (developer != null)
				bug.setCreator(developer);
			developer = (Developer) solverDeveloper.getValue();
			if (developer != null)
				bug.setSolver(developer);
			developer = (Developer) advisorDeveloper.getValue();
			if (developer != null)
				bug.setAdvisor(developer);
			bug.setStatus((String) status.getValue());
			bug.setResult((String) result.getValue());
			bug.setCreatetime((String) createtime.getValue());
			bug.setClosetime((String) closetime.getValue());
			if (update) {
				DemandOrBug.updateDemandOrBug(bug);
			} else {
				DemandOrBug.insertDemandOrBug(bug);
				// ownerProject.getDemandsOrBugs().add(bug);
			}
			if (bugsPage != null) {
				bugsPage.demandOrBugsTable
						.setContainerDataSource(DemandOrBugProvider
								.getBugsData(bugsPage.getProject()
										.getDemandsOrBugs()));
			}
			(window.getParent()).removeWindow(window);
		}
	}

	public Window getBugWindow() {
		return window;
	}

	public void setWindow(Window window) {
		this.window = window;
	}

	public BugsPage getBugsPage() {
		return bugsPage;
	}

	public void setBugsPage(BugsPage bugsPage) {
		this.bugsPage = bugsPage;
	}

	public Project getOwnerProject() {
		return ownerProject;
	}

	public void setOwnerProject(Project ownerProject) {
		this.ownerProject = ownerProject;
	}

	public DemandOrBug getBug() {
		return bug;
	}

	public void setBug(DemandOrBug bug) {
		this.bug = bug;
	}
}
